package com.wzy.wzyblog.web.admin;

import com.wzy.wzyblog.po.Blog;
import com.wzy.wzyblog.po.User;
import com.wzy.wzyblog.service.BlogService;
import com.wzy.wzyblog.service.TagService;
import com.wzy.wzyblog.service.TypeService;
import com.wzy.wzyblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin")
public class BlogController {
//    定义返回界面
    private static final String INPUT="admin/blogs-input";
    private static final String LIST="admin/blogs";
    private static final String REDIRECT_LIST="redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

//    返回类表页面
    @GetMapping("/blogs")
//                                                           时间排序
    public String blogs(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog, Model model){
//                                    加载所有type分类数据
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return LIST;
    }

//            博客中间片段区域刷新返回
    @PostMapping("/blogs/search")
//                                                           时间排序
    public String search(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
//          返回bloglist这个片段（前端定义）
        return "admin/blogs :: blogList";
    }

//    跳转发布博客页面

    @GetMapping("/blogs/input")
    public String input(Model model){
        setTypeAndTag(model);
//        初始化界面，获取一些值
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    //    修改发布博客页面
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        setTypeAndTag(model);
        Blog blog=blogService.getBlog(id);
        blog.init();
//        初始化界面，获取一些值                             根据id获取blog页面
        model.addAttribute("blog",blog);
        return INPUT;//返回到新增的页面
    }
//公共部分
    private void setTypeAndTag(Model model) {
//        初始化标签和分类
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
    }




//    保存发布信息
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
        blog.setUser((User) session.getAttribute("user"));
//                                       获取type的id
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b;
//        根据id调用方法
        if (blog.getId() == null) {
            b =  blogService.saveBlog(blog);
        } else {
            b = blogService.updateBlog(blog.getId(), blog);
        }
//              校验
        if (b == null ) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return REDIRECT_LIST;
    }



    //    删除日志
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return REDIRECT_LIST;
    }


}
